/**
 * 
 */
package com.antilia.infomaresme.pages.alert.edit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.form.EnumChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import com.antilia.infomaresme.EnumDropDownChoice;
import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.data.dataobjects.Category;
import com.antilia.infomaresme.data.dataobjects.CategoryAlert;
import com.antilia.infomaresme.data.dataobjects.CategoryEventSubject;
import com.antilia.infomaresme.data.dataobjects.Frequency;
import com.antilia.infomaresme.data.dataobjects.User;
import com.antilia.infomaresme.form.ShinyForm;
import com.antilia.infomaresme.pages.RoundSubmitLinkPanel;
import com.antilia.infomaresme.panels.filter.CategoryFilterPanel;

/**
 * @author Ernesto
 *
 */
public abstract class CategoryAlertEditPanel extends Panel {

	private static final long serialVersionUID = 1L;

	private CategoryFilterPanel filter;
	
	private List<CategoryEventSubject> events;
	
	private CategoryAlert alert;
	
	private  Label categoryPath;
	
	private  Label geographicPath;
	/**
	 * @param id
	 */
	public CategoryAlertEditPanel(String id, IModel<User> model, CategoryAlert alert) {
		super(id);
		setOutputMarkupId(true);
		
		this.alert = alert;
		if(this.alert == null) {
			this.alert = new CategoryAlert();
			this.alert.setUser(model.getObject());
			this.events = new ArrayList<CategoryEventSubject>();
		} else {
			this.events = alert.getEventsAsEnum();			
		}
		
		Form<CategoryAlert>  form = new ShinyForm<CategoryAlert>("form", new CompoundPropertyModel<CategoryAlert>(this.alert));
		add(form);
		
		form.add(new TextField<String>("title").setRequired(true));				
		form.add(new EnumDropDownChoice<Frequency>("emailFrequency", Frequency.class).setRequired(true));
		
		
		filter = new CategoryFilterPanel("filter") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onCategorySelected(AjaxRequestTarget target, Category category, Category place) {
				CategoryAlertEditPanel.this.alert.setCategoryPath(category);
				CategoryAlertEditPanel.this.alert.setGeographicPath(place);
				target.add(categoryPath);
				target.add(geographicPath);
			}
		};
		
		form.add(filter);
		
		form.add(categoryPath = new Label("categoryPath", new AbstractReadOnlyModel<String>() {
			private static final long serialVersionUID = 1L;

			@Override
			public String getObject() {
				return CategoryAlertEditPanel.this.alert.getCategoryTitle();
			}
		})); 
		form.add(geographicPath = new Label("geographicPath", new AbstractReadOnlyModel<String>() {
			private static final long serialVersionUID = 1L;

			@Override
			public String getObject() {
				return CategoryAlertEditPanel.this.alert.getGeographicLocation();
			}
		}));  
		categoryPath.setOutputMarkupId(true);
		geographicPath.setOutputMarkupId(true);
		
		List<CategoryEventSubject> EVENTS = Arrays.asList(CategoryEventSubject.values());
		
		CheckBoxMultipleChoice<CategoryEventSubject> events = new CheckBoxMultipleChoice<CategoryEventSubject>("events", EVENTS);
		events.setChoiceRenderer(new EnumChoiceRenderer<CategoryEventSubject>(this));
		events.setRequired(true);
		events.setDefaultModel(new PropertyModel<CategoryEventSubject>(this, "events"));
		form.add(events);
		
		RoundSubmitLinkPanel save = new RoundSubmitLinkPanel("save", new ResourceModel("actions.save")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				try {
					CategoryAlertEditPanel.this.alert.setEvents(CategoryAlertEditPanel.this.events);
					InfomaresmeApplication.getQAApplication().getCategoryAlertsDao().save(CategoryAlertEditPanel.this.alert);					
					onSaved(target, form);
				} catch (org.hibernate.exception.ConstraintViolationException e) {
					form.error(getString("errors.categoryAlertExist"));
					target.add(form);
				}
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				target.add(CategoryAlertEditPanel.this);				
			}
		};
		form.add(save);
	}
	
	public abstract void onSaved(AjaxRequestTarget target, Form<?> form);

	public List<CategoryEventSubject> getEvents() {
		return events;
	}

	public void setEvents(List<CategoryEventSubject> events) {
		this.events = events;
	}

}
